<?php

namespace SDXApi\Common\Tools;

/**
 * Class AppVersion
 * @author Jerry
 * @package SDXApi\Common\Tools
 */
class AppVersion
{
    /**
     * 获取app版本
     * @author Jerry
     */
    public static function getAppVersion()
    {
        return self::getVersionData();
    }

    /**
     * 判断app版本
     * @author Jerry
     * @param string $appName
     * @param string $version
     * @param string $operator
     * @return bool|mixed
     */
    public static function checkAppVersion($appName = '', $version = '', $operator = '<=')
    {
        $data = self::getAppVersion();

        if ($data['app'] == $appName) {
            return version_compare($data['version'], $version, $operator);
        }

        return false;
    }

    /**
     * 判断来源端
     * @param string $appName
     * @return bool
     */
    public static function checkAppName($appName = '')
    {
        $appData = self::getAppVersion();

        if ($appData['app'] == $appName) {
            return true;
        }

        return false;
    }

    /**
     * @author Jerry
     * @return array
     */
    protected static function getVersionData()
    {
        $is_app = I('post.is_app');
        $is_ios = I('post.is_ios');
        $is_h5 = I('post.is_h5');
        $version = I('post.version');

        if ($is_app) {
            return array(
                'app' => 'android',
                'version' => $version,
            );
        }

        if ($is_ios) {
            return array(
                'app' => 'ios',
                'version' => $version,
            );
        }

        if ($is_h5) {
            return array(
                'app' => 'h5'
            );
        }

        return array();
    }
}